package ptumall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ptumall.dao.shop.GoodsDao;
import ptumall.model.shop.*;
import ptumall.utils.BusinessException;
import ptumall.utils.PageResult;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Service
@RequiredArgsConstructor
public class GoodsService {
    private final GoodsDao goodsDao;

    @Transactional(readOnly = true)
    public GoodsDetail getGoodsDetail(Long goodsId) {
        GoodsDetail detail = goodsDao.selectGoodsBase(goodsId);
        if (detail == null) {
            throw new BusinessException("商品不存在");
        }

        // 并行查询关联数据
        CompletableFuture<List<GoodsSpec>> specFuture = CompletableFuture
                .supplyAsync(() -> goodsDao.selectSpecsByGoodsId(goodsId));

        CompletableFuture<List<GoodsSku>> skuFuture = CompletableFuture
                .supplyAsync(() -> goodsDao.selectSkusByGoodsId(goodsId));

        CompletableFuture<List<String>> galleryFuture = CompletableFuture
                .supplyAsync(() -> goodsDao.selectGalleryByGoodsId(goodsId));

        try {
            detail.setSpecs(specFuture.get());
            detail.setSkus(skuFuture.get());
            detail.setGalleryUrls(galleryFuture.get());
        } catch (InterruptedException | ExecutionException e) {
            Thread.currentThread().interrupt();
            throw new BusinessException("商品信息加载失败");
        }

        return detail;
    }

    public void validateSku(Long goodsId, Long skuId) {
        GoodsSku sku = goodsDao.selectSkuById(skuId);
        if (sku == null || !sku.getGoodsId().equals(goodsId)) {
            throw new BusinessException("无效的商品规格");
        }
        if (sku.getStock() <= 0) {
            throw new BusinessException("该规格库存不足");
        }
    }

    // Service层方法修正
    public PageResult<GoodsItem> searchGoods(GoodsSearch params) {
        int offset = (params.getPage() - 1) * params.getPageSize();

        List<GoodsItem> list = goodsDao.searchGoods(
                params.getKeyword(),
                offset,
                params.getPageSize()
        );

        Long total = goodsDao.countSearchGoods(params.getKeyword());

        return new PageResult<>(
                total,                   // Long
                params.getPage(),        // Integer
                params.getPageSize(),    // Integer
                list                     // List<GoodsItem>
        );
    }

    public PageResult<Goods> search(String keyword, int page, int pageSize) {
        // 使用PageHelper分页
        PageHelper.startPage(page, pageSize);
        List<Goods> list = goodsDao.getsearchGoods(keyword, 1);

        // 转换为PageResult
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return new PageResult<>(
                pageInfo.getTotal(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                pageInfo.getList()
        );
    }


}
